package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.earlywarn.EarlyWarnContext;
import kd.bos.entity.earlywarn.warn.plugin.IEarlyWarnCustomReceiver;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.List;

public class loanBillReceiver implements IEarlyWarnCustomReceiver {

    private static Log log = LogFactory.getLog(loanBillReceiver.class);
    private static String className = "提款处理预警插件日志-";

    @Override
    public List<Long> getReceiverIds(EarlyWarnContext earlyWarnContext, DynamicObject[] dynamicObjects) {
        List<Long> receiverIds = new ArrayList<>();
        for (DynamicObject object : dynamicObjects) {
            log.info(className + "执行符合预警的提款处理单数：" + dynamicObjects.length);
            /**查询对应的提款处理单**/
            DynamicObject loanBillObj = BusinessDataServiceHelper.loadSingleFromCache(object.getPkValue(),"cfm_loanbill");
            log.info(className + "提款处理单号：" + loanBillObj.getString("billno") + ",提款处理单ID：" + loanBillObj.getPkValue());
            if (loanBillObj!=null){
                /**借款人**/
                DynamicObject org = loanBillObj.getDynamicObject("org");
                log.info(className + "借款人：【" + org.getString("name") + "】" + "借款人编号：【" +
                        org.getString("number") + "】,借款人ID：【" + org.getPkValue() + "】");
                /**查询组织财务部长维护单**/
                QFilter[] orgFinMinisterFilter = new QFilter("billstatus", QCP.equals, "C").toArray();
                DynamicObject[] orgFinMinisterObj = BusinessDataServiceHelper.load("nckd_orgfinminister","id",orgFinMinisterFilter,"createtime desc");
                if (orgFinMinisterObj!=null && orgFinMinisterObj.length>0){
                    /**取最新的一条数据**/
                    DynamicObject orgFinMinister = BusinessDataServiceHelper.loadSingle(orgFinMinisterObj[0].getPkValue(), "nckd_orgfinminister");
                    log.info(className + "组织财务部长维护单号：【" + orgFinMinister.getString("billno") +
                            "】,组织财务部长维护单ID：【" + orgFinMinister.getPkValue() + "】");
                    DynamicObjectCollection entryentitys = orgFinMinister.getDynamicObjectCollection("entryentity");
                    for (DynamicObject entryentity:entryentitys) {
                        DynamicObject nckdOrg = entryentity.getDynamicObject("nckd_org");
                        log.info(className + "组织财务部长维护单_分录组织：【" + nckdOrg.getString("name") + "】" +
                                "组织财务部长维护单_分录组织编号：【" + nckdOrg.getString("number") + "】," +
                                "组织财务部长维护单_分录组织ID：【" + nckdOrg.getPkValue() + "】");
                        if (org.getString("number").equals(nckdOrg.getString("number"))){
                            DynamicObject finMinister = entryentity.getDynamicObject("nckd_finminister");
                            DynamicObject cashier = entryentity.getDynamicObject("nckd_cashier");
                            log.info(className + "要接收信息的组织财务部长维护单_分录人员：【" + finMinister.getString("name") + "】" +
                                    "要接收信息的组织财务部长维护单_分录人员编号：【" + finMinister.getString("number") + "】," +
                                    "要接收信息的组织财务部长维护单_分录人员ID：【" + finMinister.getPkValue() + "】");
                            log.info(className + "要接收信息的组织财务部长维护单_分录出纳：【" + cashier.getString("name") + "】" +
                                    "要接收信息的组织财务部长维护单_分录出纳编号：【" + cashier.getString("number") + "】," +
                                    "要接收信息的组织财务部长维护单_分录出纳ID：【" + cashier.getPkValue() + "】");
                            receiverIds.add((Long) finMinister.getPkValue());
                            if(finMinister.getPkValue() != cashier.getPkValue()){
                                receiverIds.add((Long) cashier.getPkValue());
                            }
                        }
                    }
                }else{
                    log.info(className + "未查询到组织财务部长维护单信息");
                }
            }
        }
        log.info("最终返回的接收消息人员数量：" + receiverIds.size());
        return receiverIds;
    }
}
